package com.hxzy.controller;

import com.hxzy.common.R;
import com.hxzy.dto.BountyOrderDto;
import com.hxzy.entity.BountyOrder;
import com.hxzy.entity.PageBean;
import com.hxzy.service.BountyOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bounty")
@Slf4j
public class BountyOrderController {

    @Autowired
    private BountyOrderService bountyOrderService;

    //分页查询
    @GetMapping("/page")
     public R<PageBean<BountyOrderDto>> selectByPage(int page, int pageSize, BountyOrder bountyOrder){
        log.info("进入selectByPage()方法...." );
        PageBean<BountyOrderDto> pageBean = bountyOrderService.selectByPageAndCondition(page, pageSize, bountyOrder);
        return R.success(pageBean);
    }

    //删除
    @DeleteMapping
    public R<String> deleteBounty(@RequestParam List<Long> ids){
        log.info("进入 deleteBounty() ...");
        boolean b = bountyOrderService.deleteBounty(ids);
        if (b){
            return R.success("删除成功!");
        }
        else {
            return R.error("删除失败!");
        }
    }


    //根据id查询
    @GetMapping("/{id}")
    public R<BountyOrder> selectById(@PathVariable Long id){
        log.info("进入 selectById 方法 ...");
        BountyOrder bountyOrder = bountyOrderService.selectById(id);
        return R.success(bountyOrder);
    }

    //修改
    @PutMapping
    public R<String> update(@RequestBody BountyOrder bountyOrder){
        log.info("进入update方法...");
        boolean b = bountyOrderService.updateBounty(bountyOrder);
        if (b) {
        return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }


    //增加
    @PostMapping
    public R<String> addBounty(@RequestBody BountyOrder bountyOrder){
        log.info("进入...addBounty方法...");
        bountyOrderService.insertBounty(bountyOrder);
        return R.success("发布悬赏令成功!");
    }

    //修改状态
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,Long id){
    log.info("进入 updateStatus 方法...");
    bountyOrderService.updateStatus(id,status);
    return R.success("状态更新成功!");
    }


}
